import json

import redis

from credit_voice_service.settings import DM_REDIS_PORT, DM_REDIS_PASSWORD, DM_REDIS_HOST


class RedisHelper:
    def __init__(self, host, port, db, password):
        self.host = host
        self.port = port
        self.db = db
        self.password = password
        # self.r = redis.Redis(host=self.host, port=self.port, db=self.db, password=self.password)
        self.connection = redis.StrictRedis(host=self.host, port=self.port, db=self.db, password=self.password,
                                            decode_responses=True, charset='UTF-8', encoding='UTF-8')

    def set(self, key, value):
        self.connection.set(key, value)

    def setStringValueWithExpire(self, key, value, expire):
        self.connection.set(key, value, ex=expire)

    def setListRightPush(self, key, value):
        self.connection.rpush(key, value)

    def getListLeftPop(self, key):
        return self.connection.lpop(key)

    def get(self, key):
        return self.connection.get(key)

    def delete(self, key):
        self.connection.delete(key)

    def keys(self, pattern):
        return self.connection.keys(pattern)

    def get_redis_conn(self):
        return self.connection


redis_helper = RedisHelper(DM_REDIS_HOST, DM_REDIS_PORT, 0, DM_REDIS_PASSWORD)


def get_redis_conn():
    return redis_helper.get_redis_conn()


if __name__ == '__main__':
    # host = "dm-8vbhbqm1mjard4hw4epd.redis.zhangbei.rds.aliyuncs.com"
    # port = "6379"
    # password = "Lingxird12345!@#"
    # redis_conn = get_redis_conn()
    # dialogueSession = redis_conn.get("dialogueSession:55369:0-2199-665520301-1666618116842831872-1264")
    # if dialogueSession is not None:
    #     session = json.loads(dialogueSession)
    #     print(111)
    # print("111")
    pass
